package com.example.springboot.service.impl;;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.common.PageParam;
import com.example.springboot.entity.Poet;
import com.example.springboot.entity.PoetVo;
import com.example.springboot.mapper.PoetMapper;
import com.example.springboot.service.IPoetService;
import com.example.springboot.vo.CountLocaleVO;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class PoetServiceImpl extends ServiceImpl<PoetMapper, Poet> implements IPoetService{

    @Override
    public boolean save(Poet entity) {
        return super.save(entity);
    }

    @Override
    public boolean updateById(Poet entity) {
        return super.updateById(entity);
    }

    @Override
    public boolean removeById(Poet entity) {
        return super.removeById(entity);
    }

    @Override
    public boolean removeBatchByIds(Collection<?> list) {
        return super.removeBatchByIds(list);
    }

    @Override
    public List<Poet> list() {
        return super.list();
    }

    @Override
    public Poet getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public <E extends IPage<Poet>> E page(E page, Wrapper<Poet> queryWrapper) {
        return super.page(page, queryWrapper);
    }

    @Override
    public List<PoetVo> getList() {
        return this.baseMapper.getList();
    }

    @Override
    public List<Poet> getAgeList() {
        return this.baseMapper.getAgeList();
    }

    @Override
    public CountLocaleVO countLocale(PageParam param) {
        CountLocaleVO countLocaleVO = new CountLocaleVO();
        // 出生地
        QueryWrapper birthWrapper = new QueryWrapper<>()
                .select("birth_locale AS locale", "COUNT(*) AS number")  // 替换target_field为实际字段名
                .groupBy("birth_locale")
                .orderByDesc("number");
        Page page = this.page(param, birthWrapper);
        List<Poet> birthList = page.getRecords();
        countLocaleVO.setBirth(birthList);
        // 死亡
        QueryWrapper deathWrapper = new QueryWrapper<>()
                .select("death_locale AS locale", "COUNT(*) AS number")  // 替换target_field为实际字段名
                .groupBy("death_locale")
                .orderByDesc("number");
        Page deathPage = this.page(param, deathWrapper);
        List<Poet> deathList = deathPage.getRecords();
        countLocaleVO.setDeath(deathList);
        return countLocaleVO;
    }

    @Override
    public List<Poet> countSurname(PageParam param) {
        QueryWrapper wrapper = new QueryWrapper<>()
                .select("surname", "COUNT(*) AS number")  // 替换target_field为实际字段名
                .groupBy("surname")
                .orderByDesc("number");
        Page deathPage = this.page(param, wrapper);
        List<Poet> list = deathPage.getRecords();
        return list;
    }

    @Override
    public List<Map<String,String>> getNameMap(){
        List<Map<String,String>> mapList = new ArrayList<>();
        List<String> birthLocaleList = this.baseMapper.getBirthLocaleList();
        List<String> birthProvinceList = this.baseMapper.getBirthProvinceList();
        List<String> deathProvinceList = this.baseMapper.getDeathProvinceList();
        List<String> deathLocaleList = this.baseMapper.getDeathLocaleList();
        List<String> strings = new ArrayList<>();
        strings.addAll(birthLocaleList);
        strings.addAll(birthProvinceList);
        strings.addAll(deathProvinceList);
        strings.addAll(deathLocaleList);
        strings.forEach(s -> {
            Map<String,String> map = new HashMap<>();
            map.put("name",s);
            mapList.add(map);
        });
        return mapList;
    }

    /**
     * 获取地图数据
     * 该方法用于获取包含出生地和死亡地信息的映射列表
     * 主要目的是为了生成可视化地图数据，展示诗人出生地和死亡地的分布情况
     *
     * @return 包含出生地和死亡地信息的映射列表
     */
    @Override
    public List<Map<String,Integer>> getMap() {
        // 初始化字符串列表，用于存储出生地和省份信息
        List<String> strings = new ArrayList<>();
        // 初始化映射列表，用于存储最终的映射数据
        List<Map<String,Integer>> mapList = new ArrayList<>();

        // 获取出生地列表
        List<String> birthLocaleList = this.baseMapper.getBirthLocaleList();
        // 获取出生地及其对应数量的列表
        List<PoetVo> birthLocaleListCount = this.baseMapper.getBirthLocaleListCount();
        // 将出生地及其数量转换为映射
        Map<String, Integer> birthLocaleListCountMap = birthLocaleListCount.stream().collect(Collectors.toMap(PoetVo::getName, PoetVo::getValue));

        // 获取出生省份列表
        List<String> birthProvinceList = this.baseMapper.getBirthProvinceList();
        // 获取出生省份及其对应数量的列表
        List<PoetVo> birthProvinceListCount = this.baseMapper.getBirthProvinceListCount();
        // 将出生省份及其数量转换为映射
        Map<String, Integer> birthProvinceListCountMap = birthProvinceListCount.stream().collect(Collectors.toMap(PoetVo::getName, PoetVo::getValue));

        // 获取死亡地及其对应数量的列表
        List<PoetVo> deathLocaleListCount = this.baseMapper.getDeathLocaleListCount();
        // 将死亡地及其数量转换为映射
        Map<String, Integer> deathLocaleListCountMap = deathLocaleListCount.stream().collect(Collectors.toMap(PoetVo::getName, PoetVo::getValue));
        // 获取死亡省份及其对应数量的列表
        List<PoetVo> deathProvinceListCount = this.baseMapper.getDeathProvinceListCount();
        // 将死亡省份及其数量转换为映射
        Map<String, Integer> deathProvinceListCountMap = deathProvinceListCount.stream().collect(Collectors.toMap(PoetVo::getName, PoetVo::getValue));

        // 将出生地和出生省份添加到字符串列表中
        strings.addAll(birthLocaleList);
        strings.addAll(birthProvinceList);

        // 获取诗人列表
        List<Poet> list = this.list(new LambdaQueryWrapper<Poet>().orderByAsc(Poet::getBirthLocale));
        // 获取死亡省份和死亡地列表
        List<String> deathProvinceList = this.baseMapper.getDeathProvinceList();
        List<String> deathLocaleList = this.baseMapper.getDeathLocaleList();

        // 获取字符串列表的大小
        int size = strings.size();
        Set<String> uniqueLinks = new HashSet<>();
        // 如果诗人列表不为空，则遍历每个诗人，生成映射数据
        if (list.size() >0){
            list.forEach(poet -> {
                // 初始化一个新的字符串列表，用于存储死亡省份和死亡地信息
                List<String> stringList = new ArrayList<>();
                stringList.addAll(deathProvinceList);
                stringList.addAll(deathLocaleList);



                // 如果诗人的出生地在出生地映射中，则添加其数量到映射中
                if (birthLocaleListCountMap.containsKey(poet.getBirthLocale())){
                    // 初始化映射，用于存储单个诗人的出生地或死亡地信息
                    Map<String,Integer> map = new HashMap<>();
                    map.put("value",birthLocaleListCountMap.get(poet.getBirthLocale()));
                    // 遍历字符串列表，找到出生地和省份的索引，添加到映射中
                    for (int i = 0; i < strings.size(); i++) {
                        if (strings.get(i).equals(poet.getBirthLocale())){
                            map.put("source",i);
                        }
                        if (strings.get(i).equals(poet.getBirthProvince())){
                            map.put("target",i);
                        }
                    }
                    String key = "出生"+poet.getBirthLocale()+"到"+poet.getBirthProvince();
                    if (!uniqueLinks.contains(key)){
                        uniqueLinks.add(key);
                        // 将生成的映射添加到映射列表中
                        mapList.add(map);
                    }
                }

                // 如果诗人的出生省份在出生省份映射中，则添加其数量到映射中
                if (birthProvinceListCountMap.containsKey(poet.getBirthProvince())){
                    // 初始化映射，用于存储单个诗人的出生地或死亡地信息
                    Map<String,Integer> map = new HashMap<>();
                    map.put("value",birthProvinceListCountMap.get(poet.getBirthProvince()));
                    // 遍历字符串列表，找到出生省份的索引，添加到映射中
                    for (int i = 0; i < strings.size(); i++) {
                        if (strings.get(i).equals(poet.getBirthProvince())){
                            map.put("source",i);
                        }
                    }
                    // 遍历字符串列表，找到死亡省份的索引，添加到映射中
                    for (int i = 0; i < stringList.size(); i++){
                        if (stringList.get(i).equals(poet.getDeathProvince())){
                            map.put("target",i+size);
                        }
                    }
                    String key = "出生-死亡"+poet.getBirthProvince()+"到"+poet.getDeathProvince();
                    if (!uniqueLinks.contains(key)){
                        uniqueLinks.add(key);
                        // 将生成的映射添加到映射列表中
                        mapList.add(map);
                    }
                }

                // 如果诗人的死亡地在死亡地映射中，则添加其数量到映射中
                if (deathProvinceListCountMap.containsKey(poet.getDeathProvince())){
                    // 初始化映射，用于存储单个诗人的出生地或死亡地信息
                    Map<String,Integer> map = new HashMap<>();
                    map.put("value",deathProvinceListCountMap.get(poet.getDeathProvince()));
                    // 遍历字符串列表，找到死亡省份和死亡地的索引，添加到映射中
                    for (int i = 0; i < stringList.size(); i++) {
                        if (stringList.get(i).equals(poet.getDeathProvince())){
                            map.put("source",i+size);
                        }
                        if (stringList.get(i).equals(poet.getDeathLocale())){
                            map.put("target",i+size);
                        }
                    }
                    String key = "死亡"+poet.getDeathProvince()+"到"+poet.getDeathLocale();
                    if (!uniqueLinks.contains(key)){
                        uniqueLinks.add(key);
                        // 将生成的映射添加到映射列表中
                        mapList.add(map);
                    }
                }

//                // 如果诗人的死亡地在死亡地映射中，则添加其数量到映射中
//                if (deathLocaleListCountMap.containsKey(poet.getDeathLocale())){
//                    // 初始化映射，用于存储单个诗人的出生地或死亡地信息
//                    Map<String,Integer> map = new HashMap<>();
//                    map.put("value",deathLocaleListCountMap.get(poet.getDeathLocale()));
//                    // 遍历字符串列表，找到死亡地和死亡省份的索引，添加到映射中
//                    for (int i = 0; i < stringList.size(); i++) {
//                        if (stringList.get(i).equals(poet.getDeathLocale())){
//                            map.put("source",i+size);
//                        }
//                        if (stringList.get(i).equals(poet.getDeathProvince())){
//                            map.put("target",i+size);
//                        }
//                    }
//                    String key = "死亡省份"+poet.getDeathLocale()+"到"+poet.getDeathProvince();
//                    if (!uniqueLinks.contains(key)){
//                        uniqueLinks.add(key);
//                        // 将生成的映射添加到映射列表中
//                        mapList.add(map);
//                    }
//                }


            });
        }

        // 返回映射列表
        return mapList;
    }
}
